﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using APPLICATION.Dict;
namespace APPLICATION.Dict.City
{

    public partial class FrmCity : Form
    {
        bool isDefaultText;
        public FrmCity()
        {            
            InitializeComponent();            
        }

        private void ShowFormNewCity(int CodeID, int level)
        {            
            FrmNewCity FrmNewCity1 = new FrmNewCity(CodeID, level);                
            FrmNewCity1.ShowDialog(this);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataSet1.FillDtCity();
            bindingSource1.DataSource = dataSet1.DtCity;
            treeList1.DataSource = bindingSource1;            
            isDefaultText = true;
        }

        private void countryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowFormNewCity(0, 0);
        }

        private void regionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowFormNewCity(Convert.ToInt32((object)this.treeList1.Selection[0].GetValue("CodeID")),1);
        }

        private void cityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowFormNewCity(Convert.ToInt32((object)this.treeList1.Selection[0].GetValue("CodeID")),2);
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (treeList1.Selection.Count > 0)
            {
                //false=0, true=1 or more than 1
                toolStripMenuItemBtnCity.Enabled = Convert.ToBoolean(treeList1.Selection[0].Level);                
                cityToolStripMenuItem1.Enabled = Convert.ToBoolean(treeList1.Selection[0].Level);
            }

            toolStripMenuItemBtnRegion.Enabled = Convert.ToBoolean(treeList1.Selection.Count);

            buttonEdit.Enabled = Convert.ToBoolean(treeList1.Selection.Count);
            buttonDetete.Enabled = Convert.ToBoolean(treeList1.Selection.Count); 


            //context menu
            удалитьToolStripMenuItem.Enabled = Convert.ToBoolean(treeList1.Selection.Count);
            редактироватToolStripMenuItem.Enabled = Convert.ToBoolean(treeList1.Selection.Count);

        }


        private void textBoxSearch_Enter(object sender, EventArgs e)
        {
            if (isDefaultText)
            {
                textBoxSearch.Text = "";
                textBoxSearch.ForeColor = Color.Black;
            }
        }

        private void textBoxSearch_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxSearch.Text))
            {
                textBoxSearch.Text = "Search by name";
                textBoxSearch.ForeColor = Color.Gray;
                isDefaultText = true;
            }
            else
            {
                isDefaultText = false;
                
            }
        }

        bool isKeyPressed;
        private void textBoxSearch_KeyDown(object sender, KeyEventArgs e)
        {
            isKeyPressed = true;
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            if (isKeyPressed)
            {
                bindingSource1.Filter = "hName like '%" + textBoxSearch.Text + "%'";                 
            }
            if (isKeyPressed && string.IsNullOrEmpty(textBoxSearch.Text))
            {
                bindingSource1.Filter = "";                
            }
            isKeyPressed = false;
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (treeList1.Selection.Count > 0)
            {
                FrmUpdCity FrmUpdCity1 = new FrmUpdCity(Convert.ToInt32((object)this.treeList1.Selection[0].GetValue("CodeID")), treeList1.Selection[0].Level);
                FrmUpdCity1.ShowDialog(this);
            }
        }

        private void buttonDetete_Click(object sender, EventArgs e)
        {
            if (treeList1.Selection.Count > 0)
            {
                if (MessageBox.Show(this, "Would you like delete?", "Country", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    dataSet1.DeleteData(Convert.ToInt32(this.treeList1.Selection[0].GetValue("CodeID")));
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
